package com.loli;

import java.util.Map;

import org.nutz.json.Json;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.constants.Constants;
import com.model.HttpResponseObject;
import com.utils.HttpUtils;

public class LoliImageActivity extends Activity {

	// 按钮
	private Button loginBtn;
	private Button regBtn;
	// 文本框
	private EditText name;
	private EditText password;

	int toastTime = Toast.LENGTH_SHORT;
	
	ProgressDialog pd;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		loginBtn = (Button) findViewById(R.id.loginBtn);
		regBtn = (Button) findViewById(R.id.regBtn);
		name = (EditText) findViewById(R.id.nameText);
		password = (EditText) findViewById(R.id.pwdText);
		loginBtn.setOnClickListener(new StartBtnOnClickListener());
		regBtn.setOnClickListener(new RegBtnOnClickListener());
	}

	// 注册按钮的点击
	class RegBtnOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			Toast toast = new Toast(LoliImageActivity.this);
			toast.setText(R.string.reg_null);
			toast.show();
		}
	}

	// 登录按钮的点击
	class StartBtnOnClickListener implements OnClickListener {

		@SuppressWarnings("unchecked")
		@Override
		public void onClick(View v) {
			String pass = name.getText().toString();
			String pwd = password.getText().toString();
			if (HttpUtils.isEmptyString(pass)) {
				Toast.makeText(LoliImageActivity.this, R.string.passport_empty,
						toastTime).show();
				return;
			}
			if (HttpUtils.isEmptyString(pwd)) {
				Toast.makeText(LoliImageActivity.this, R.string.password_empty,
						toastTime).show();
				return;
			}
			String reg = "^[0-9a-zA-Z_@.]+$";
			if (!HttpUtils.isMatch(reg, pass)) {
				Toast.makeText(LoliImageActivity.this, R.string.passport_error,
						toastTime).show();
				return;
			}
			if (!HttpUtils.isMatch(reg, pwd)) {
				Toast.makeText(LoliImageActivity.this, R.string.password_error,
						toastTime).show();
				return;
			}
			pd = ProgressDialog.show(LoliImageActivity.this, "查询中", "正在登录，请稍候……");
			HttpResponseObject hro = HttpUtils
					.getInfoFromUrl(Constants.USER_LOGIN_URL + "pass=" + pass
							+ "&pwd=" + pwd);
			String resContent = hro.getResContent();
			Map<String, Object> map = (Map<String, Object>) Json
					.fromJson(resContent);
			int state = (Integer) map.get("state");
			if (state == 0) {
				Toast.makeText(LoliImageActivity.this,
						map.get("info").toString(), toastTime).show();
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("passport", pass);
				intent.putExtras(bundle);
				intent.setClass(LoliImageActivity.this, ImgListActivity.class);
				startActivity(intent);
			} else {
				Toast.makeText(LoliImageActivity.this,
						map.get("info").toString(), toastTime).show();
			}
			pd.hide();
		}

	}
}